define(function(require, exports, module) {

  // var Backbone = require('backbone');
  var $ = require('jquery');
  var _ = require('lodash');
  // var jsonview = require('jsonview');
  var tmpl = require('text!../tmpl.html');

  function main() {
    var $app = $('<div id="app">');
    $('#main').html($app);

    $app.append(tmpl);

    // console.log(huiWenShuzi())

    // var astr = ["10", "0001", "111001", "1", "0"];
    // const ret = zeroAndOne(astr, 5, 3)

    // console.log(ret.arr.map(index => astr[index]))

    var astr = ["10", "0", "1"];
    const ret = zeroAndOne(astr, 1, 1)

    console.log(ret.arr.map(index => astr[index]))

  }


  // 打印1~10000内所有的回文数字，如121，2442
  function huiWenShuzi() {
    function isMatch(num) {
      var numStr = String(num);
      var reverseStr = numStr.split('').reverse().join('')
      if (numStr === reverseStr) {
        return true
      } else {
        return false
      }
    }

    var results = []
    for (var i = 11; i <= 10000; i++) {
      if (isMatch(i)) {
        results.push(i)
      }
    }
    return results;
  }

  /**
   * 给你一个二进制字符串数组 strs 和两个整数 m 和 n 。
   * 请你找出并返回 strs 的最大子集的大小，该子集中 最多 有 m 个 0 和 n 个 1
   */
  function zeroAndOne(astr, m, n) {

    function measureCount(str) {
      var len = str.length;
      var count = [0,0]
      for (var i = 0; i < len; i++) {
        if (str.charAt(i) === '0') {
          count[0]++
        } else {
          count[1]++
        }
      }
      return count;
    }
    var len = astr.length;

    var dp = []

    for (var count0 = 0; count0 <= m; count0++) {
      dp[count0] = []

      for (var count1 = 0; count1 <= n; count1++) {
        dp[count0][count1] = []

        for (var i = 0; i < len; i++) {
          var onlyPrev = dp[count0][count1][i-1] || { count: 0 }
          var onlyPrevCount = onlyPrev.count

          var [currentCount0, currentCount1] = measureCount(astr[i])
          
          if (currentCount0 <= count0 && currentCount1 <= count1) {
            var prev = dp[count0-currentCount0][count1-currentCount1][i-1] || { count: 0 };
            var prevAndCurrentCount = prev.count + 1;

            if (prevAndCurrentCount > onlyPrevCount) {
              dp[count0][count1][i] = {
               count: prevAndCurrentCount,
               arr: (prev.arr || []).concat([i])
              }
              continue;
            }
          }
          
          dp[count0][count1][i] = {
           count: onlyPrevCount,
           arr: (onlyPrev.arr || []).slice()
          }
        }
      }
    }

    return dp[m][n][len-1]
  }
  

  return main;
})
